package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.DrawRecordAdapter;
import flc.ast.bean.MyDrawRecordBean;
import flc.ast.databinding.ActivityDrawRecordBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.UriUtil;

public class DrawRecordActivity extends BaseAc<ActivityDrawRecordBinding> {

    private DrawRecordAdapter recordAdapter;
    private List<MyDrawRecordBean> listShow = new ArrayList<>();
    private Dialog myMoreDialog;
    private String selPath = "";
    private Dialog myClearDialog;

    @Override
    protected int onCreate() {
        return R.layout.activity_draw_record;
    }

    @Override
    protected void initView() {
        mDataBinding.ivDrawRecordBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.rvDrawRecordList.setLayoutManager(new GridLayoutManager(mContext, 2));
        recordAdapter = new DrawRecordAdapter();
        mDataBinding.rvDrawRecordList.setAdapter(recordAdapter);
        recordAdapter.addChildClickViewIds(R.id.ivDrawRecordItemMore);
        recordAdapter.setOnItemChildClickListener(this);
        mDataBinding.ivDrawRecordDelete.setOnClickListener(this);
        //更多弹框
        moreDialog();
        //清空弹框
        clearDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivDrawRecordDelete://清空
                if (listShow.size() == 0) return;
                myClearDialog.show();
                break;
            //更多弹框
            case R.id.tvDialogMoreShare://分享
                myMoreDialog.dismiss();
                sharePicture(selPath);
                break;
            case R.id.tvDialogMoreDownload://下载
                myMoreDialog.dismiss();
                saveToAlbum();
                break;
            case R.id.tvDialogMoreDelete://删除
                myMoreDialog.dismiss();
                deletePicture();
                break;
            case R.id.tvDialogMoreCancel://取消
                myMoreDialog.dismiss();
                break;
            //清空弹框
            case R.id.ivDialogClearCancel://取消
                myClearDialog.dismiss();
                break;
            case R.id.ivDialogClearRight://确定
                myClearDialog.dismiss();
                delete();
                break;
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivDrawRecordItemMore://更多
                selPath = recordAdapter.getItem(position).getPath();
                myMoreDialog.show();
                break;
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        listShow.clear();
        //获取文件夹下文件
        String path = PathUtils.getFilesPathExternalFirst() + Content.myDraw;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(new MyDrawRecordBean(file.getPath(), FileUtils.getFileName(file.getPath()), FileUtils.getSize(file.getPath()), TimeUtils.millis2String(FileUtils.getFileLastModified(file.getPath()), new SimpleDateFormat("yyyy/MM/dd"))));
            }
        }
        if (listShow.size() > 0) {
            recordAdapter.setList(listShow);
            mDataBinding.rvDrawRecordList.setVisibility(View.VISIBLE);
            mDataBinding.ivDrawRecordNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvDrawRecordList.setVisibility(View.GONE);
            mDataBinding.ivDrawRecordNoData.setVisibility(View.VISIBLE);
        }
    }

    //分享图片
    private void sharePicture(String path) {
        Uri uri = UriUtil.path2Uri(mContext, path);
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        startActivity(Intent.createChooser(intent, "Share"));
    }

    //保存到相册
    private void saveToAlbum() {
        showDialog(getString(R.string.download_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        FileP2pUtil.copyPrivateImgToPublic(mContext, selPath);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String bitmap) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.download_suc);
                    }
                });
            }
        }, 500);
    }

    //删除图片
    private void deletePicture() {
        showDialog(getString(R.string.delete_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        FileUtils.delete(selPath);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String bitmap) {
                        dismissDialog();
                        getData();
                    }
                });
            }
        }, 500);
    }

    // 清空
    private void delete() {
        showDialog(getString(R.string.delete_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        for (MyDrawRecordBean bean : listShow) {
                            FileUtils.delete(bean.getPath());
                        }
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        getData();
                        dismissDialog();
                    }
                });
            }
        }, 500);
    }

    //更多弹框
    private void moreDialog() {
        myMoreDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_more, null);
        myMoreDialog.setContentView(inflate);
        myMoreDialog.setCancelable(false);
        Window dialogWindow = myMoreDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        lp.y = 80;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView dialogShare = inflate.findViewById(R.id.tvDialogMoreShare);
        TextView dialogSave = inflate.findViewById(R.id.tvDialogMoreDownload);
        TextView dialogDelete = inflate.findViewById(R.id.tvDialogMoreDelete);
        TextView dialogCancel = inflate.findViewById(R.id.tvDialogMoreCancel);
        dialogShare.setOnClickListener(this);
        dialogSave.setOnClickListener(this);
        dialogDelete.setOnClickListener(this);
        dialogCancel.setOnClickListener(this);
    }

    //清空弹框
    private void clearDialog() {
        myClearDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_clear, null);
        myClearDialog.setContentView(inflate);
        myClearDialog.setCancelable(false);
        Window dialogWindow = myClearDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.8);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView dialogCancel = inflate.findViewById(R.id.ivDialogClearCancel);
        ImageView dialogRight = inflate.findViewById(R.id.ivDialogClearRight);
        dialogCancel.setOnClickListener(this);
        dialogRight.setOnClickListener(this);
    }

}